package com.bw.module_motorcircle.test_recyclerview.diffi

import android.os.Bundle
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bw.lib_common.utils.ToastUtils
import com.bw.lib_image.GlideApp
import com.bw.module_motorcircle.R
import com.bw.module_motorcircle.test_recyclerview.MyAdapter
import com.bw.module_motorcircle.test_recyclerview.MyModel

class DiffActivity : AppCompatActivity() {
    private lateinit var rv: RecyclerView
    private lateinit var adapter: MyDiffAdapter
    private val mList = ArrayList<MyModel>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_diff)
        rv = findViewById(R.id.rv)
        rv.setHasFixedSize(true) //设置item固定高度，较少requestLayout
        adapter = MyDiffAdapter(
            onImageClick = {position: Int ->
                ToastUtils.showLong(this,"点击了"+position)
            }
        )
        rv.adapter = adapter
        rv.layoutManager = LinearLayoutManager(this)

        for (i in 0..100){
            val myModel = MyModel(i,"title"+i,"https://ts1.tc.mm.bing.net/th/id/R-C.98411f1b4f70501fbfa652c4c2a63843?rik=BI5K%2bIqMmK3d5g&riu=http%3a%2f%2fimg.ewang.com%2f6c117a240949949b1c6c.jpg&ehk=g9TE7x8dPR2TpuAjguBK7KayNfHi0eI2GY%2bn7SEEzPE%3d&risl=&pid=ImgRaw&r=0")
            mList.add(myModel)
        }
        adapter.setData(mList)

        //滑动卡顿优化：滑动停止图片的加载，滑动暂停图片的加载
        rv.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                if (newState == RecyclerView.SCROLL_STATE_IDLE){//滑动暂停
                    GlideApp.with(this@DiffActivity).resumeRequests()
                }else{
                    GlideApp.with(this@DiffActivity).pauseRequests()
                }

            }
        })


    }

    fun setNewData(view: View) {
        val mList = ArrayList<MyModel>()
        for (i in 0..100){
            val myModel = MyModel(i,"新的title"+i,"https://ts1.tc.mm.bing.net/th/id/R-C.98411f1b4f70501fbfa652c4c2a63843?rik=BI5K%2bIqMmK3d5g&riu=http%3a%2f%2fimg.ewang.com%2f6c117a240949949b1c6c.jpg&ehk=g9TE7x8dPR2TpuAjguBK7KayNfHi0eI2GY%2bn7SEEzPE%3d&risl=&pid=ImgRaw&r=0")
            mList.add(myModel)
        }
        adapter.setData(mList)
    }
}